<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Comments/classes/PhpFox_ServiceComment.class.php, $Author:  $ */



/** Service for working with comments
 *
 * @package module.comments
 * @version $Id: PhpFox_ServiceComment.class.php 428 2008-04-11 09:24:52Z phpfoxsv $
 *
 */
class PhpFox_Mod_Comments_ServiceComment extends BaseService
{
    function onDeleteAccount($aParams)
    {
        $aIds = $aParams['aIds'];
        
        foreach ($aIds as $iUserId)
        {
            $sSql = 'DELETE FROM '.App::getT('comments').' WHERE userid = "'.$iUserId.'"';
            $this->oDatabase->query($sSql);
        }
    }

    /* Now use onItemDelete($iItemId, $sType)
    function onPictureDelete($iPictureId)
    {
        $oCommentItem = &$this->_oModule->getItem('Comment');

        $iPictureId = intval($iPictureId);
        $sSql = 'DELETE FROM '.App::getT('comments').' '.
               ' WHERE typeid = "'. App::getParam('aCommentId',"gallery") .'" '.
               '   AND itemid = '.$iPictureId;
        return $oCommentItem->oDb->query($sSql);
    }

    function onBlogDelete($iBlogId)
    {
        $oCommentItem = &$this->_oModule->getItem('Comment');

        $iBlogId = intval($iBlogId);
        $sSql = 'DELETE FROM '.App::getT('comments').' '.
               ' WHERE typeid = "'. App::getParam('aCommentId',"blog") .'" '.
               '   AND itemid = '.$iBlogId;
        return $oCommentItem->oDb->query($sSql);
    }
    */

	function onDelete($iId)
	{
		$oCommentItem = &$this->_oModule->getItem('Comment');
        /* @var $oCommentItem PhpFox_Mod_Comments_ItemComment */
		$oAccountService = &App::getModuleService('Account', 'Account');
        $sSql = "SELECT  userid FROM ". App::getT('comments') ." WHERE cid = '". $iId ."'";
        $sComentUser = $oCommentItem->oDb->getField($sSql);
		$this->oDatabase->query("DELETE FROM ". App::getT('comments') ." WHERE cid = '". $iId ."'");
        $oAccountService->changeUserInfo($oAccountService->getById($sComentUser), 'comment', -1);
	}

    /** Checks, that flood time is expired
     *
     * @return boolean
     */
    function checkFlood()
    {
        $iTime = time() - App::getSetting('flood_time');

       $checkCount = $this->oDatabase->getRow('SELECT time '.
			' FROM '.App::getT('comments').' '.
			' WHERE userid = "'. App::getUser('id') .'" ORDER BY time DESC LIMIT 0,1');

		if ( isset($checkCount['time']) AND (($checkCount['time'] + App::getSetting('flood_time')) >= time()) )
		{
			return false;
		}

		return true;
    }

    /** Get comment count for comment item
     *
     * @param string $sItem may be: gallery, blog, profile
     * @param integer $iId
     * @return integer
     */
    function getCommentsCount($sItem, $iId)
    {
        $oCommentItem = &$this->_oModule->getItem('Comment');
        /* @var $oCommentItem PhpFox_Mod_Comments_ItemComment */

        $iId = intval($iId);
        switch ($sItem)
        {
            case 'gallery':
                $sSql = 'SELECT COUNT(*) '.
                       ' FROM '.App::getT('comments').' '.
                       ' WHERE typeid = "'. App::getParam('aCommentId',"gallery") .'"'.
                       ' WHERE itemid = '.$iId;
                $iCount = $oCommentItem->oDb->getField($sSql);
                break;
            case 'blog':
                $sSql = 'SELECT COUNT(*) '.
                       ' FROM '.App::getT('comments').' '.
                       ' WHERE typeid = "'. App::getParam('aCommentId',"blog") .'"'.
                       ' WHERE itemid = '.$iId;
                $iCount = $oCommentItem->oDb->getField($sSql);
                break;
            case 'profile':
                $sSql = 'SELECT COUNT(*) '.
                       ' FROM '.App::getT('comments').' '.
                       ' WHERE typeid = "'. App::getParam('aCommentId',"account") .'"'.
                       ' WHERE itemid = '.$iId;
                $iCount = $oCommentItem->oDb->getField($sSql);
                break;
        }
        return $iCount;
    }

	function addVideoComment($iId, $sText)
	{
		$aQuery = array
		(
			'cm_mainid' => $iId,
			'cm_userid' => App::getUser('id'),
			'cm_text' => $sText,
			'cm_time' => time()
		);

		$iCid = $this->oDatabase->insert(App::getT('videos_comments'), $aQuery);

    	$iCnt = $this->oDatabase->getRow("SELECT COUNT(*) AS count FROM ". App::getT('videos_comments') ." WHERE cm_mainid = '". $iId ."'");
		$this->oDatabase->query("UPDATE ". App::getT('videos') ." SET comments = '". $iCnt['count'] ."' WHERE vid_id = '". $iId ."'");

		return $iCid;
	}

	function addComment($typeid, $itemid, $userid, $sValues, $iMainUserId = "")
	{
		if ( $typeid == '6' )
		{
			$iCid = $this->addVideoComment($itemid, $sValues);
		}
		else
		{
			$aQuery = array
			(
				'typeid' => $typeid,
				'itemid' => $itemid,
				'userid' => $userid,
				'text' => $sValues,
				'time' => time(),
			);

			$iCid = $this->oDatabase->insert(App::getT('comments'), $aQuery);
		}

		if ( $iMainUserId AND ($userid!=$iMainUserId))
		{
			$oSrvMes = &App::getModuleService('Messenger', 'Messenger');

			$oSrvMes->addMailAlert(array('type' => $typeid, 'item' => $itemid, 'to_id' => $iMainUserId, 'c_id' => $iCid));
		}
		
		return $iCid;
	}

    function updateCounts($sTable, $sField, $sQuery, $typeid, $itemid)
    {
    	$iCnt = $this->oDatabase->getRow("SELECT COUNT(*) AS count FROM ". App::getT('comments') ." WHERE typeid = '". $typeid ."' AND itemid = '". $itemid ."' ");
		$this->oDatabase->query("UPDATE ". App::getT($sTable) ." SET ". $sField ." = '". $iCnt['count'] ."' WHERE ". $sQuery ."");
		return $iCnt;
    }
    
	/**
	 * process for delete some item with comments (but not video item)
	 *
	 * @param int 		$iItemId	item ID
	 * @param string 	$sType		type of item
	 * @return boolean				is comments was deleted
	 */
	function onItemDelete($iItemId, $sType)
    {
        $iItemId = intval($iItemId);
        $aComentUsers = $this->_getUsersForComments($sType,$iItemId);
        $sSql = 'DELETE FROM '.App::getT('comments').' '.
               ' WHERE typeid = "'. App::getParam('aCommentId',$sType) .'" '.
               '   AND itemid = '.$iItemId;
        $bRes = $this->oDatabase->query($sSql);
        $this->_updateUserInfo($aComentUsers);
        return $bRes;
    }
    
	function onVideoDelete($iItemId)
    {
        $iItemId = intval($iItemId);
        $aComentUsers = $this->_getUsersForVideoComments($iItemId);
        $sSql = 'DELETE FROM '.App::getT('videos_comments').' '.
               ' WHERE cm_mainid = '.$iItemId;
        $bRes = $this->oDatabase->query($sSql);
        $this->_updateUserInfo($aComentUsers);
        return $bRes;
    }
    
    function _getUsersForComments($sType,$iItem)
    {
    	$sSql = 'SELECT  cid, userid FROM '.App::getT('comments').' '.
               ' WHERE typeid = "'. App::getParam('aCommentId',$sType) .'" '.
               '   AND itemid = '.$iItem;
         return $this->oDatabase->getHash($sSql);
        
    }

	function _getUsersForVideoComments($iItem)
    {
    	$sSql = 'SELECT  cm_id, cm_userid FROM '.App::getT('videos_comments').' '.
               ' WHERE  cm_mainid = '.$iItem;
         return $this->oDatabase->getHash($sSql);
    }
    
    function _updateUserInfo($aComentUsers)
    {
    	$oAccountService = &App::getModuleService('Account', 'Account');
        foreach($aComentUsers as $iUserId)
        {
        	$oAccountService->changeUserInfo($oAccountService->getById($iUserId), 'comment', -1, false);
        }
    }
}
?>